package windows;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.Vector;

import main.ImageLibrary;
import main.Telecommando;

import base.Button;
import base.Window;

public class Thermostat extends Window{

	private Vector<Button> buttons = new Vector<Button>( );
	
	Font textFont  		= new Font( "TextFont" , Font.PLAIN , 14 );
	Font tempFont  		= new Font( "TempFont" , Font.PLAIN , 64 );
	Font desiredFont  	= new Font( "DesiredFont" , Font.PLAIN , 32 );
	
	private int currentTemp = 70;
	private int desiredTemp = 70;
	
	private int tStatus = 0;	//0: off, 1: heat, 2: cool
	private int fStatus = 0;	//0: off, 1: on, 2: auto
	
	private int counter = 0;	//used to approximate time
	
	public Thermostat( ) {
		
		super( "Thermostat" , "Favorites" , "Uncategorized" );
		
		//Add images to library
		ImageLibrary.addImage( "button_auto" , 	"Images/Thermostat/button_auto.png" );
		ImageLibrary.addImage( "button_cool" , 	"Images/Thermostat/button_cool.png" );
		ImageLibrary.addImage( "button_down" , 	"Images/Thermostat/button_down.png" );
		ImageLibrary.addImage( "button_heat" , 	"Images/Thermostat/button_heat.png" );
		ImageLibrary.addImage( "button_off" , 	"Images/Thermostat/button_off.png" );
		ImageLibrary.addImage( "button_on" , 	"Images/Thermostat/button_on.png" );
		ImageLibrary.addImage( "button_up" , 	"Images/Thermostat/button_up.png" );
		
		//Create buttons
		buttons.add( new Button( "Heat" , 	new Point( 5 , 0 ) , 		"Images/Thermostat/button_heat.png" , false , true ) );
		buttons.add( new Button( "A/C" , 	new Point( 55 , 0 ) , 		"Images/Thermostat/button_cool.png" , false , true ) );
		buttons.add( new Button( "TOff" , 	new Point( 105 , 0 ) , 		"Images/Thermostat/button_off.png" , false , true ) );
		
		buttons.add( new Button( "TUp" , 	new Point( 106 , 67 ) , 	"Images/Thermostat/button_up.png" , false , true ) );
		buttons.add( new Button( "TDown" , 	new Point( 106 , 180 ) , 	"Images/Thermostat/button_down.png" , false , true ) );
		
		buttons.add( new Button( "FOn" , 	new Point( 5 , 245 ) , 		"Images/Thermostat/button_on.png" , false , true ) );
		buttons.add( new Button( "FOff" , 	new Point( 55 , 245 ) , 	"Images/Thermostat/button_off.png" , false , true ) );
		buttons.add( new Button( "Auto" , 	new Point( 105 , 245 ) ,	"Images/Thermostat/button_auto.png" , false , true ) );

	}

	@Override
	public void draw(Graphics2D g2d) {
		for( Button but : buttons )
			but.draw( g2d );
		
		//Labels
		g2d.setColor(Color.WHITE);
		g2d.setFont(textFont);
		g2d.drawString( "Current Temp:  " , 5 , 125 );
		g2d.drawString( "Thermostat:  " , 10 , 70 );
		g2d.drawString( "Fan Status:  " , 10 , 215 );
		g2d.drawString( "Set To:" , 105 , 135 );
		
		//Dividing Lines
		g2d.setColor(Color.GRAY);
		g2d.drawLine( 5 , 195 , 95 , 195 );		//bottom left
		g2d.drawLine( 5 , 100 , 95 , 100 );		//top left
		g2d.drawLine( 105 , 60 , 155 , 60 );	//top right
		g2d.drawLine( 105 , 235 , 155 , 235 );	//bottom right
		g2d.drawLine( 100 , 65 , 100 , 230 );	//vertical
		
		//Counter
		//g2d.drawString( "" + counter , 100 , 0 );
		
		//Thermostat and Fan status
		if( tStatus == 0 ){
			g2d.setColor(Color.WHITE);
			g2d.drawString( "OFF" , 30 , 90 );
		}
		else if( tStatus == 1 ){
			g2d.setColor(Color.RED);
			g2d.drawString( "HEAT" , 30 , 90 );
		}
		else if( tStatus == 2 ){
			g2d.setColor(Color.cyan);
			g2d.drawString( "COOL" , 30 , 90 );
		}
		
		if( fStatus == 0 ){
			g2d.setColor(Color.WHITE);
			g2d.drawString( "OFF" , 30 , 235 );
		}
		else if( fStatus == 1 ){
			g2d.setColor(Color.GREEN);
			g2d.drawString( "ON" , 30 , 235 );
		}
		else if( fStatus == 2 ){
			g2d.setColor(Color.cyan);
			g2d.drawString( "AUTO" , 30 , 235 );
		}
		
		//Temperatures
		g2d.setColor(Color.WHITE);
		g2d.setFont(tempFont);
		g2d.drawString( "" + currentTemp + '\u00B0' , 0 , 180 );
		
		if( desiredTemp > currentTemp && tStatus == 1 ){
			g2d.setColor(Color.RED);
		}
		else if( desiredTemp < currentTemp && tStatus == 2 ){
			g2d.setColor(Color.CYAN);
		}
		else{
			g2d.setColor(Color.WHITE);
		}
		g2d.setFont(desiredFont);
		g2d.drawString( "" + desiredTemp + '\u00B0' , 105 , 170 );
		
		//Increment counter and change current temperature
		counter++;
		if( counter % 100 == 0 ) {
			if( ( tStatus == 1 ) && ( currentTemp < desiredTemp ) ) {
				currentTemp++;
			}
			else if( ( tStatus == 2 ) && ( currentTemp > desiredTemp ) ) {
				currentTemp--;
			}
		}
		
	}

	@Override
	public void fingerPressed(Point location) {
		for( Button but : buttons )
		{
			if( but.press( location.x , location.y ) ) {
				//System.out.println("Pressed " + but.getName());
				if( but.getName().equals("Heat") ) {
					tStatus = 1;
				}
				else if( but.getName().equals("A/C") ) {
					tStatus = 2;
				}
				else if( but.getName().equals("TOff") ) {
					tStatus = 0;
				}
				else if( but.getName().equals("FOff") ) {
					fStatus = 0;
				}
				else if( but.getName().equals("FOn") ) {
					fStatus = 1;
				}
				else if( but.getName().equals("Auto") ) {
					fStatus = 2;
				}
				else if( but.getName().equals("TUp") ) {
					desiredTemp++;
				}
				else if( but.getName().equals("TDown") ) {
					desiredTemp--;
				}
				return;
			}
		}		
	}

	@Override
	public void fingerReleased(Point location) {
		for( Button but : buttons )
			but.release( );
		
	}

	@Override
	public void gestureHorizontal(int amount) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void gestureVertical(int amount) {
		// TODO Auto-generated method stub
		
	}

}
